package com.hxiaoyi.cloud.controller;

import com.hxiaoyi.cloud.entities.CommonResult;
import com.hxiaoyi.cloud.entities.Payment;
import com.hxiaoyi.cloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * PaymentController
 * @author 黄敬意
 * @date 2022-02-02 14:24
 **/
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    /**获取端口号*/
    @Value("${server.port}")
    private String serverPort;

    /**服务发现信息，还需要再主启动类上加上@EnableDiscoveryClient这个注解*/
    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 创建支付
     * @author 黄敬意
     * @date 2022/2/2 19:58
     * @param payment
     * @return CommonResult
     **/
    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("----插入结果：" + result);
        if(result > 0){
            return new CommonResult(200,"插入数据库成功，serverPort:" + serverPort, result);
        }else {
            return new CommonResult(444,"插入数据库失败", null);
        }
    }
    
    /**
     * 根据id查询支付
     * @author 黄敬意
     * @date 2022/2/2 19:57
     * @param id
     * @return CommonResult
     **/
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("----根据id查询结果：" + payment);
        if(payment != null ){
            return new CommonResult(200,"查询成功，serverPort：" + serverPort, payment);
        }else {
            return new CommonResult(444,"查询失败没有对应的记录，查询id：" + id, null);
        }
    }

    @GetMapping(value = "/payment/discovery")
    public Object discovery(){
        /**获取eureka服务列表名称*/
        List<String> services = discoveryClient.getServices();
        for (String dis : services) {
            log.info("服务列表名称：" + dis);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("服务id：" + instance.getServiceId() + ", 主机地址host：" + instance.getHost()
                    + ", 端口号port：" + instance.getPort() + ", uri地址：" + instance.getUri());
        }
        return  this.discoveryClient;
    }

    /**获取服务端口号*/
    @GetMapping(value = "/payment/lb")
    public String getPaymentLb() {
        return serverPort;
    }

    /**
     * feign超时演示
     */
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try {
            /**耗时5秒钟*/
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    /**获取服务端口号*/
    @GetMapping(value = "/payment/zipkin")
    public String paymentZipkin() {
        return "--------这是一个链路监控的zipkin----------" + serverPort;
    }
}

